<?php

namespace HelloWeb\UserBundle\Entity\Repository;

use HelloWeb\UserBundle\Entity\User;
use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository
{
    /**
     * Load all users by conditions
     * @param null $searchArray
     * @param bool $allowActive user
     * @param bool $allowDeleted user
     * @return null|User[]
     */
    public function fetchAll($searchArray = null, $allowDeactive = false, $allowDeleted = false)
    {
        $q = $this->getEntityManager()->createQueryBuilder()
        ->select('dc')
        ->from('HelloWebUserBundle:User', 'dc');
    
        // Show active users only?
        if (!$allowDeactive) {
            $q->andWhere('dc.isActive = 1');
        }
    
        // Show Deleted?
        if (!$allowDeleted) {
            $q->andWhere('dc.deleted = 0');
        }
        // Search criteria?
        if (is_array($searchArray)) {
            if (array_key_exists('text', $searchArray)) {
                $q->andWhere('dc.firstName LIKE :searchText OR dc.lastName LIKE :searchText');
                $q->setParameter('searchText', '%'. $searchArray['text'] .'%');
            }
        }
        $q->addOrderBy('dc.firstName','ASC');
        $q->addOrderBy('dc.lastName','ASC');
        return $q->getQuery()->getResult();
    }
}